package com.sz.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.gcord.gcorddemo.R;

import cn.com.geartech.gcordsdk.GcordSDK;
import cn.com.geartech.gcordsdk.PhoneAPI;

public class DialActivity extends AppCompatActivity {

    Button btn_dial;
    EditText edit_phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dial1);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        btn_dial = (Button)findViewById(R.id.btn_dial);
        edit_phoneNum = (EditText)findViewById(R.id.edit_phoneNumber);

        btn_dial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberToDial = edit_phoneNum.getText().toString();
                if(numberToDial.length() > 0)
                {
                    //拨打电话
                    GcordSDK.getInstance().getPhoneAPI().dial(numberToDial);//固话
//                    GcordSDK.getInstance().getCellPhoneManager().makeCall(numberToDial);//手机
                    btn_dial.setEnabled(false);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            btn_dial.setEnabled(true);
                        }
                    }, 3000);
                }
            }
        });

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        GcordSDK.getInstance().getPhoneAPI().addPhoneEventListener(phoneEventListener);

        super.onResume();
    }

    @Override
    protected void onPause() {
        GcordSDK.getInstance().getPhoneAPI().removePhoneEventListener(phoneEventListener);

        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    PhoneAPI.PhoneEventListener phoneEventListener = new PhoneAPI.PhoneEventListener() {
        @Override
        public void onPickUp(PhoneAPI.PICKUP_STATE pickup_state) {

            Intent intent = new Intent(DialActivity.this, InCallActivity.class);
            intent.putExtra(InCallActivity.CALL_TYPE, InCallActivity.CALL_TYPE_OUT);
            startActivity(intent);

            btn_dial.setEnabled(true);
        }

        @Override
        public void onInComingCall() {

        }

        @Override
        public void onRingEnd() {

        }

        @Override
        public void onPhoneNumberReceived(String s) {

        }

        @Override
        public void onSwitchPhoneState(PhoneAPI.PICKUP_STATE pickup_state) {

        }

        @Override
        public void onHangOff() {

        }
    };
}
